package cn.cvs.controller;

import cn.cvs.pojo.StorageRecord;
import cn.cvs.pojo.Supplier;
import cn.cvs.pojo.SysRole;
import cn.cvs.pojo.SysUser;
import cn.cvs.service.StorageRecordService;
import cn.cvs.service.SupplierService;
import cn.cvs.utils.Constants;
import cn.cvs.utils.PageSupport;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
public class StorageRecordController {

    // 入库
    @Autowired
    private StorageRecordService storageRecordService;

 // 供货商
    @Autowired
    private SupplierService supplierService;


    // 分页查询入库记录信息
    @RequestMapping("/sys/storageRecord/list")
    public String findByAccountOrRoleId(@RequestParam(value = "queryGoodsName", required = false) String queryGoodsName,
                                        @RequestParam(value = "querySupplierId", required = false) Integer querySupplierId,
                                        @RequestParam(value = "queryPayStatus", required = false) Integer queryPayStatus,
                                        @RequestParam(defaultValue="1", required = false) Integer pageIndex,
                                        Model model) {


        try{

            // 设置页面大小
            int pageSize = Constants.pageSize;
            //总记录数
            int  totalCount = storageRecordService.getUserCount("%"+(queryGoodsName == null ? "":queryGoodsName)+"%",querySupplierId,queryPayStatus);

            PageSupport pages = new PageSupport();
            pages.setCurrentPageNo(pageIndex);
            pages.setPageSize(pageSize);
            pages.setTotalCount(totalCount);
            int totalPageCount = pages.getTotalPageCount();
            //控制首尾页
            if (pageIndex > totalCount){
                pageIndex = totalPageCount;
            }


            List<StorageRecord> storageRecordList = storageRecordService.findStorageRecord("%"+(queryGoodsName == null ? "":queryGoodsName)+"%",querySupplierId,queryPayStatus,pageIndex,pageSize);
            List<Supplier> supplierList = supplierService.findAllSupplier(); // 获取所有供货商列表
            for (Supplier supplier : supplierList) {
                System.out.println(supplier);
            }
            model.addAttribute("supplierList", supplierList); // 将供货商列表添加到模型
            model.addAttribute("storageRecordList", storageRecordList); // 将用户列表添加到模型
            model.addAttribute("queryGoodsName",queryGoodsName);
            model.addAttribute("querySupplierId",querySupplierId);
            model.addAttribute("queryPayStatus",queryPayStatus);
            model.addAttribute("totalPageCount",totalPageCount);
            model.addAttribute("totalCount",totalCount);
            model.addAttribute("currentPageNo",pageIndex);
            return "storageRecord/list"; // 返回用户列表视图

        }catch (Exception e){
            e.printStackTrace();
            return "redirect:/sysError";
        }



    }


    // 查看供货商信息
    @RequestMapping("/sys/storageRecord/view/{id}")
    public String getByIdOrStorageRecord(@PathVariable("id") Integer id, Model model) {
        StorageRecord storageRecord = storageRecordService.getByIdOrStorageRecord(id);
        model.addAttribute("storageRecord",storageRecord);
        return "storageRecord/view"; // 返回查看供货商列表视图
    }


    // 根据编号删除入库信息
    @RequestMapping(value="/sys/storageRecord/del/{id}",produces="application/json;charset=UTF-8")
    @ResponseBody
    public  String delSupplier(@PathVariable("id") Integer id, Map<String,String> map ){
        int count = storageRecordService.delStorageRecord(id);
        String delResult = "false";
        if (count > 0){
            delResult = "true";
        }else if ( count  == 0){
            delResult = "notexist";
        }
        map.put("delResult",delResult);
        String data = JSON.toJSONString(map);
        return data;

    }


     //跳转添加
     @RequestMapping("/sys/storageRecord/toAdd")
     public String toAdd(){
        return"storageRecord/add";
     }

     // 添加入库信息
    @RequestMapping("/sys/storageRecord/add")
    public  String addStorageRecord(StorageRecord storageRecord, HttpSession session){

        // 创建人id
        storageRecord.setCreatedUserId(((SysUser)session.getAttribute(Constants.USER_SESSION)).getId());// 获取创建人id

        int count = storageRecordService.addStorageRecord(storageRecord);

        if( count > 0){
            return "redirect:/sys/storageRecord/list";
        }
        return "storageRecord/add";

    }






    // 根据编号获取入库信息
    @RequestMapping("/sys/storageRecord/toUpdate/{id}")
    public String getByIdOrStorageRecors(@PathVariable("id") Integer id, Model model) {
        StorageRecord storageRecord = storageRecordService.getByIdOrStorageRecord(id);
        model.addAttribute("storageRecord",storageRecord);
        return "storageRecord/update";
    }


    // 修改入库信息
    @RequestMapping("/sys/storageRecord/update")
    public  String updateStorageRecord(StorageRecord storageRecord, HttpSession session){

        // 修改人id
        storageRecord.setUpdatedUserId(((SysUser)session.getAttribute(Constants.USER_SESSION)).getId());// 获取创建人id
        // 修改时间
        storageRecord.setUpdatedTime(new Date());

        int count = storageRecordService.updateStorageRecord(storageRecord);

        if( count > 0){
            return "redirect:/sys/storageRecord/list";
        }

        return "storageRecord/update";

    }
}
